package com.aiti.lulian.gxmh.service.impl;

import cn.hutool.core.util.StrUtil;
import com.aiti.base.core.exception.BusinessException;
import com.aiti.base.core.page.PageInfo;
import com.aiti.base.core.page.PageResult;
import com.aiti.lulian.gxmh.dto.GxmhCustomerDetailParamDto;
import com.aiti.lulian.gxmh.dto.GxmhCustomerPageParamDto;
import com.aiti.lulian.gxmh.vo.GxmhCustomerContactsVo;
import com.aiti.lulian.gxmh.vo.GxmhCustomerDetailVo;
import com.aiti.lulian.gxmh.vo.GxmhCustomerFollowUpRecordVo;
import com.aiti.lulian.gxmh.vo.GxmhCustomerVo;
import com.aiti.lulian.mapper.gxmh.GxmhCustomerMapper;
import com.aiti.lulian.utils.BusinessUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * description: 工信门户客户管理
 * auth fcb
 * date 2025-06-30 15:35
 **/
@Service
@Slf4j
public class GxmhCustomerService {

    @Autowired
    private GxmhCustomerMapper gxmhCustomerMapper;

    /**
     * 客户管理列表
     * @param dto
     * @return
     */
    public PageResult<GxmhCustomerVo> queryCustomerList(GxmhCustomerPageParamDto dto) {
        BusinessUtil.checkPageParam(dto);
        if(StrUtil.isBlank(dto.getCompanyId())) {
            throw new BusinessException("企业标识不能为空");
        }
        IPage<GxmhCustomerVo> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        List<GxmhCustomerVo> list = gxmhCustomerMapper.queryCustomerList(page, dto);
        PageResult<GxmhCustomerVo> result = new PageResult<>(list, new PageInfo(dto.getPageNum(), dto.getPageSize()), page.getTotal());
        return result;
    }

    /**
     * 客户详情
     * @param baseId
     * @return
     */
    public GxmhCustomerDetailVo queryCustomerDetail(String baseId) {
        if(StrUtil.isBlank(baseId)) {
            throw new BusinessException("客户标识不能为空");
        }
        return gxmhCustomerMapper.queryCustomerDetail(baseId);
    }

    /**
     * 客户联系人列表
     * @param customerId
     * @return
     */
    public List<GxmhCustomerContactsVo> queryCustomerContactsList(String customerId) {
        if(StrUtil.isBlank(customerId)) {
            throw new BusinessException("客户标识不能为空");
        }
        return gxmhCustomerMapper.queryCustomerContactsList(customerId);
    }

    /**
     * 修改跟进状态
     * @param baseId
     * @param followStatus
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateFollowStatus(String baseId, String followStatus, String userId) {
        if(StrUtil.isBlank(baseId)) {
            throw new BusinessException("客户标识不能为空");
        }
        if(StrUtil.isBlank(followStatus)) {
            throw new BusinessException("跟进状态不能为空");
        }
        gxmhCustomerMapper.updateFollowStatus(baseId, followStatus, userId);
    }

    /**
     * 修改客户信息
     * @param dto
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateCustomer(GxmhCustomerDetailParamDto dto) {
        if(StrUtil.isBlank(dto.getBaseId())) {
            throw new BusinessException("客户标识不能为空");
        }
        gxmhCustomerMapper.updateCustomer(dto);
    }

    /**
     * 客户跟进记录列表
     * @param customerId
     * @return
     */
    public List<GxmhCustomerFollowUpRecordVo> queryCustomerFollowUpRecordList(String customerId) {
        if(StrUtil.isBlank(customerId)) {
            throw new BusinessException("客户标识不能为空");
        }
        return gxmhCustomerMapper.queryCustomerFollowUpRecordList(customerId);
    }
}
